package main

import (
	"fmt"
	"go-web-learning/src/04_gorm/db"
	"gorm.io/gorm"
)

var DB = db.DB

func main() {
	// 子查询
	var students []db.Student
	//DB.Where("age > (?)", DB.Model(db.Student{}).Select("avg(age)")).Find(&students)
	//fmt.Println(students)

	//// 命名参数
	//DB.Where("name = @name", sql.Named("name", "老李")).
	//	Where("age = @age", map[string]any{"age": 26}).
	//	Find(&students)
	//fmt.Println(students)

	//// find到map
	//var res []map[string]any
	//DB.Table("students").Find(&res)
	//fmt.Println(res)

	// 使用scopes
	DB.Model(db.Student{}).Scopes(AgeGt23).Scopes(GenderGtWoman).Find(&students)
	fmt.Println(students)
}

func AgeGt23(db *gorm.DB) *gorm.DB {
	return db.Where("age > ?", 23)
}

func GenderGtWoman(db *gorm.DB) *gorm.DB {
	return db.Where("gender = ?", "女")
}
